/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JComponent;
import javax.swing.JTextField;
import model.Base;
import model.StaffMember;
import model.Student;
import model.Teacher;
import view.NewUserWindow;

/**
 *
 * @author Ira
 */
public class NewUserController implements ActionListener, FocusListener{

	private NewUserWindow view;
	private Base model;
	
	public NewUserController(Base model, NewUserWindow view) {
		this.model = model;
		this.view = view;
		this.verifData();
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		String btn = ((JComponent)e.getSource()).getName();
		if(btn.equals(NewUserWindow.BTN_CANCEL)) {
			this.view.dispose();
		}
		else if(btn.equals(NewUserWindow.BTN_CREATE)) {
			String type = this.view.getUserType();
			int errors = this.verifData();
			if(errors == 0) {
				if(type.equals("Etudiant")) {
					this.model.addUser(new Student(this.view.getUserName(), this.view.getUserFirstName(), this.view.getUserPseudo(), this.view.getUserPass()));
				}
				else if(type.equals("Professeur")) {
					this.model.addUser(new Teacher(this.view.getUserName(), this.view.getUserFirstName(), this.view.getUserPseudo(), this.view.getUserPass()));
				}
				else if(type.equals("Bibliothécaire")) {
					this.model.addUser(new StaffMember(this.view.getUserName(), this.view.getUserFirstName(), this.view.getUserPseudo(), this.view.getUserPass()));
				}
			}
			this.view.confirmUserCreation(errors);
		}
	}

	@Override
	public void focusGained(FocusEvent e) {
		
	}

	@Override
	public void focusLost(FocusEvent e) {
		this.verifData();
	}
	
	public int verifData() {
		String text = this.view.getUserName();
		int errors = 0;
		
		// Name
		if(text.equals("")) {
			this.view.setNameInfo("doit être renseigné", true);
			++errors;
		}
		else if(text.matches("[A-Z][a-z]*")) {
			this.view.setNameInfo("valide", false);
		}
		else {
			this.view.setNameInfo("[A-Z][a-z]* autorisé", true);
			++errors;
		}

		// FirstName
		text = this.view.getUserFirstName();
		if(text.equals("")) {
			this.view.setFirstNameInfo("doit être renseigné", true);
			++errors;
		}
		else if(text.matches("[A-Z][a-z]*")) {
			this.view.setFirstNameInfo("valide", false);
		}
		else {
			this.view.setFirstNameInfo("[A-Z][a-z]* autorisé", true);
			++errors;
		}

		// Pseudo		
		if(this.view.getUserPseudo().equals("")) {
			this.view.setPseudoInfo("doit être renseigné", true);
			++errors;
		}
		else {
			if(!model.personSearch(null, null, this.view.getUserPseudo(), null).isEmpty()) {
				this.view.setPseudoInfo("Le pseudo existe déja", true);
				++errors;
			}
			else {
				this.view.setPseudoInfo("valide", false);
			}
		}
		
		// Pass
		if(this.view.getUserPass().equals("")) {
			this.view.setPassInfo("doit être renseigné", true);
			++errors;
		}
		else {
			this.view.setPassInfo("valide", false);
		}
		
		
		// Verif Pass
		if(this.view.getUserPass().equals(this.view.getUserPassVerif())) {
			this.view.setPassVerifInfo("valide", false);
		}
		else {
			this.view.setPassVerifInfo("est différent", true);
			++errors;
		}
		
		return errors;
	}
}
